﻿// ----------------------------------------------------------------------
// <author name="Moses Kehengu"/>
// <copyright file="FacilityReportInfo.cs" company="Futures Group International">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataModels.Common;
using DataModels.Enumerations;
using GenericTools;

namespace ReportingManagement
{
    public partial class FacilityReportInfo : Form
    {
        private string facilityCode;
        private string facilityName;
        private int period;
        private int year;
        private List<string> selectedPhoneNumbers;
        private List<string> excludedPhoneNumbers;

        public FacilityReportInfo(string facilityCode, string facilityName, 
            int period, int year, List<string> previousList = null)
        {
            InitializeComponent();

            this.facilityCode = facilityCode;
            this.facilityName = facilityName;
            this.period = period;
            this.year = year;

            this.lblFacilityCode.Text = this.facilityCode;
            this.lblFacilityName.Text = this.facilityName;

            string monthText = Utilities.GetMonthText(this.period);
            this.lblInfo.Text = String.Format("Reports for {0} {1}", monthText, this.year);

            this.Text = string.Format("{0}: {1}", this.facilityName, this.lblInfo.Text);

            Cursor.Current = Cursors.WaitCursor;
            DataTable dataTable = Utilities.GetReportingStatus(this.facilityCode, this.period, this.year);
            Cursor.Current = Cursors.Default;
            dgvReportingStatus.DataSource = dataTable;
            dgvReportingStatus.Columns["RepID"].Visible = false;
            dgvReportingStatus.Columns["ReportName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            Cursor.Current = Cursors.WaitCursor;
            dataTable = Utilities.GetRegisteredReportingUsers(this.facilityCode);
            Cursor.Current = Cursors.Default;
            dgvRegisteredUsers.DataSource = dataTable;
            dgvRegisteredUsers.Columns["Full name"].ReadOnly = true;
            dgvRegisteredUsers.Columns["Full name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            dgvRegisteredUsers.Columns["User Title"].ReadOnly = true;
            dgvRegisteredUsers.Columns["Cell number"].ReadOnly = true;
            dgvRegisteredUsers.Columns["Send SMS"].ReadOnly = false;

            this.excludedPhoneNumbers = new List<string>();
            this.selectedPhoneNumbers = previousList;
            if (this.selectedPhoneNumbers != null)
            {
                // Clear selection from DB
                foreach (DataGridViewRow row in dgvRegisteredUsers.Rows)
                {
                    DataGridViewCell sendSMSCell = row.Cells["Send SMS"];
                    sendSMSCell.Value = false;
                }
                // Set according to user selection
                foreach (string number in this.selectedPhoneNumbers)
                {
                    foreach (DataGridViewRow row in dgvRegisteredUsers.Rows)
                    {
                        DataGridViewCell numberCell = row.Cells["Cell number"];
                        string cellnumber = numberCell.Value.ToString();
                        if (cellnumber == number)
                        {
                            DataGridViewCell sendSMSCell = row.Cells["Send SMS"];
                            sendSMSCell.Value = true;
                        }
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        
        /// <summary>
        /// Gets the list of cell number selected to receive SMS reminder
        /// </summary>
        public List<string> SelectedPhoneNumbers
        {
            get
            {
                return this.selectedPhoneNumbers;
            }
        }

        public List<string> ExcludedPhoneNumbers
        {
            get
            {
                return this.excludedPhoneNumbers;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if ((AppConfiguration.AccessMethod == DataAccessMethod.OFFLINE_DATA_ACCESS) ||
                (AppConfiguration.AccessMethod == DataAccessMethod.OFFLINE_DIRECT_ACCESS))
            {
                string message = @"The application is running in offline mode. " +
                    "You can not perform any activities that require server " +
                    "connection while you are offline. " +
                    "\n\nYou are current operation will not be processed!";
                MessageBox.Show(this, message, AppConfiguration.ApplicationName, 
                    MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            if (this.selectedPhoneNumbers == null) this.selectedPhoneNumbers = new List<string>();
            this.selectedPhoneNumbers.Clear();

            List<string> sendingNumbers = new List<string>();
            foreach (DataGridViewRow row in dgvRegisteredUsers.Rows)
            {
                bool send = bool.Parse(row.Cells["Send SMS"].Value.ToString());
                string number = row.Cells["Cell number"].Value.ToString();

                if (send)
                {
                    this.selectedPhoneNumbers.Add(number);
                    sendingNumbers.Add(number);
                }
                else this.excludedPhoneNumbers.Add(number);
            }

            if (chbSaveToDB.Checked)
            {                
                Cursor.Current = Cursors.WaitCursor;
                btnOK.Enabled = false; Application.DoEvents();
                try
                {
                    Utilities.SaveReminderReceiver(this.facilityCode, sendingNumbers);
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage(ex);
                }
                finally
                {
                    btnOK.Enabled = true;
                    Cursor.Current = Cursors.WaitCursor;
                }
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void DisplayErrorMessage(Exception ex)
        {
            Cursor.Current = Cursors.Default;
            string subTitle = String.Empty;
            if ((AppConfiguration.AccessMethod == DataAccessMethod.OFFLINE_DATA_ACCESS) ||
                (AppConfiguration.AccessMethod == DataAccessMethod.OFFLINE_DIRECT_ACCESS))
            {
                subTitle = "Error occured during processing your request";
            }
            else
            {
                subTitle = "Please make sure you have internet connection";
            }

            string message = String.Format("{0}\n\nError Details:\n{1}", subTitle, ex.Message);
            MessageBox.Show(this, message, AppConfiguration.ApplicationName,
                MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
    }
}
